package com.example.mynews.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewpager.widget.ViewPager;


import com.example.mynews.Adapter.News_pagerAdapter;
import com.example.mynews.Adapter.listview_Adapter;
import com.example.mynews.Pojo.Pojo_Msg;
import com.example.mynews.Pojo.Pojo_banner;
import com.example.mynews.Pojo.Pojo_listview;
import com.example.mynews.Pojo.Pojo_tab;
import com.example.mynews.R;
import com.example.mynews.UI.web_content;
import com.example.mynews.Utils.Ok_Utils;
import com.example.mynews.Utils.Param_Utils;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;


public class Fragment_News extends Fragment {
    private TabLayout news_tab;
    private ViewPager f_viewpager;
    private ListView f_listview;
    private ScrollView Scroll;

    private View view;
    private Pojo_tab pojo_tab;
    private List<Pojo_listview>list;
    private Pojo_banner pojo_banner;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view = inflater.inflate(R.layout.fragment_news, container, false);
        //EventBus注册，防止多次注册
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        iniview();
        inifuction();
        return view;
    }

    private void iniview() {
        news_tab=view.findViewById(R.id.news_tab);
        f_viewpager=view.findViewById(R.id.f_viewpager);
        f_listview=view.findViewById(R.id.f_listview);
        Scroll=view.findViewById(R.id.Scroll);
    }
    private void inifuction() {
        //tab
        Ok_Utils.ok_get("/static/api/news/categories.json", Param_Utils.TAB);
       news_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
           @Override
           public void onTabSelected(TabLayout.Tab tab) {
               //点击获取所有下一页的数据
            String url = pojo_tab.getData().get(0).getChildren().get(tab.getPosition()).getUrl();
            Ok_Utils.ok_get(url, Param_Utils.BANNER);
           }
           @Override
           public void onTabUnselected(TabLayout.Tab tab) {

           }
           @Override
           public void onTabReselected(TabLayout.Tab tab) {

           }
       });

        f_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String url = pojo_banner.getData().getNews().get(i).getUrl();
                Intent intent=new Intent(getContext(), web_content.class);
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void ok_data(Pojo_Msg pojoMsg)
    {
       switch (pojoMsg.tag)
       {
           case Param_Utils.TAB:
               Gson gson=new Gson();
                pojo_tab = gson.fromJson(pojoMsg.data, Pojo_tab.class);
               for (int i = 0; i < 12; i++) {
                   String title = pojo_tab.getData().get(0).getChildren().get(i).getTitle();

                   Log.d("xx",title);
                   news_tab.addTab(news_tab.newTab().setText(title));
               }
               break;
           case Param_Utils.BANNER:
                Gson gson1=new Gson();
                pojo_banner = gson1.fromJson(pojoMsg.data, Pojo_banner.class);
               News_pagerAdapter adapter=new News_pagerAdapter(pojo_banner.getData().getTopnews(), getContext());
               f_viewpager.setAdapter(adapter);

               //listview
               list=new ArrayList<>();
               for (int i = 0; i <pojo_banner.getData().getNews().size(); i++) {
                   String listimage = pojo_banner.getData().getNews().get(i).getListimage();
                   String title = pojo_banner.getData().getNews().get(i).getTitle();
                   String pubdate = pojo_banner.getData().getNews().get(i).getPubdate();
                   list.add(new Pojo_listview(listimage,title,pubdate));
               }
               f_listview.setAdapter(new listview_Adapter(list,getContext()));
               break;
       }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
